package com.woniuxy.happylife.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xpath.internal.operations.Bool;
import com.woniuxy.happylife.dto.AddRoleEntity;
import com.woniuxy.happylife.dto.FormInLineEntity;
import com.woniuxy.happylife.entity.RbacRole;
import com.woniuxy.happylife.entity.ResponseEntity;
import com.woniuxy.happylife.service.RbacRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Classname RoleController
 * @Description TODO
 * @Version 1.0.0
 * @Date 2022/2/17 14:53
 * @Created by hang
 */
@RestController
@RequestMapping("/rolepermmenu/role")
public class RoleController {
    @Resource
    RbacRoleService rbacRoleService;
    @PostMapping("/findAll")
   public ResponseEntity<PageInfo<RbacRole>> findAll(@RequestBody FormInLineEntity<RbacRole> formInLineEntity){
//        System.out.println("进来了");
        PageHelper.startPage(formInLineEntity.getPage(),formInLineEntity.getLimit());
        List<RbacRole> roles =  rbacRoleService.findAll();
        PageInfo<RbacRole> rolePageInfo = new PageInfo<>(roles);
       return new ResponseEntity<>(200,"查询所有角色成功",rolePageInfo);
   }


   //添加一个角色
   @PostMapping("/addOne")
   public ResponseEntity<Boolean> addOne(@RequestBody AddRoleEntity addRoleEntity){
       System.out.println("劲来了"+addRoleEntity);
       int i = rbacRoleService.addOne(addRoleEntity);
       if(i>0){
           return  new ResponseEntity<Boolean>(200, "添加成功", true);
       }

       return new ResponseEntity<Boolean>(500, "角色名已存在", false);
   }
   @PostMapping("/findById")
   public ResponseEntity<String> findById(@RequestBody RbacRole rbacRole){
      int i =  rbacRoleService.findById(rbacRole);
       if(i > 0){
           return  new ResponseEntity<String>(200, "查找成功","");
       }
       return new ResponseEntity<String>(500, "查询失败", "");
   }
   @RequestMapping("/updateOne")
   public ResponseEntity<Boolean> updateOne(@RequestBody RbacRole rbacRole){
        Boolean success = rbacRoleService.updateOne(rbacRole);
        if(success){
           return  new ResponseEntity<Boolean>(200, "修改成功", true);
        }
        return new ResponseEntity<Boolean>(500, "修改失败", false);
   }
   @DeleteMapping("/deleteOneById/{id}")
   public ResponseEntity<Boolean> deleteOneById(@PathVariable("id")int id){
       System.out.println(id);
        Boolean delete = rbacRoleService.deleteOneById(id);
        if(delete){
            return  new ResponseEntity<>(200, "删除成功", null);
        }
       return new ResponseEntity<>(500, "删除失败", null);
   }


}
